#include"headers.h"
#include"system.h"
#include"player.h"
#include"picture.h"
#include"collision.h"
#include"score.h"
#include"music.h"

volatile double elasped_time;

void increase_elasped_time()
{
    elasped_time++;
}
END_OF_FUNCTION(increase_elasped_time);

int main()
{
    System Game(640,400,24,false);
    Game.Init(false);
    LOCK_VARIABLE(elasped_time);
    LOCK_FUNCTION(increase_elasped_time);
    install_int_ex(increase_elasped_time, BPS_TO_TIMER(60));

    BITMAP *buffer = create_bitmap(640,400);
    BITMAP *Text = create_bitmap(640,400);

    int p_mouse_x, p_mouse_y;

    Picture background("data\main_page.bmp");

    Picture Mouse("data\hand.bmp");

    Picture background_game("data\Classic_N64_Back.bmp");

    Player Cartrigdes[Game.enemies];

    Collision Collide;

    Music Background_music("data\bg.mid",1);
    Music Level_up("data\level up.wav",2);
    Music Grab("data\collect.wav", 2);

    Background_music.load();
    Level_up.load();
    Grab.load();
    Score HighScore(Game.enemies);

    blit(background.picture,screen,0,0,0,0,SCREEN_W,SCREEN_H);
    readkey();

    for(int i= 0; i < Game.enemies; i++) //initial settings
    {
        Cartrigdes[i].set_main(false); // sets all these players as AI's because they are
        // not controlled by the keyboard or the mouse.
        Cartrigdes[i].Set_sheet( "cartridge_set.bmp" ); // loads the sprite sheet fo all
        Cartrigdes[i].Set_sprite( rand() % 6 );
        Cartrigdes[i].alive = true;
        Cartrigdes[i].set_x( rand() % SCREEN_W - 32 );
        Cartrigdes[i].set_y( rand() % SCREEN_H - 32 );
        Cartrigdes[i].sprite = create_bitmap( 32 , 32 );
        Cartrigdes[i].speed = rand() % Game.dificulty;

        while(Cartrigdes[i].speed < 2)
        {
            Cartrigdes[i].speed = rand() % Game.dificulty;
        }
    }

    Background_music.play(true);

    while(!Game.quit) // main game loop
    {

        clear(buffer);
        while(elasped_time > 0) //game logic loop
        {
            poll_mouse();
            if(key[KEY_ESC]) //needs to be added to system class... check key_hit
            {
                Game.quit = true;
            }
            if(key[KEY_P])
            {
                cout<<"mouse_x: "<< mouse_x<<endl;
                cout<<"mouse_y: "<< mouse_y<<endl;
                cout<<"SCREEN_H: "<< SCREEN_H<<endl;
                cout<<"SCREEN_W: "<<SCREEN_W<<endl;
                cout<<"Score: "<< HighScore.score <<endl;
            }
            if(key[KEY_F1])
            {
                 for(int r = 0; r < Game.enemies; r++)
                {
                    Cartrigdes[r].set_x( rand() % SCREEN_W);
                    Cartrigdes[r].set_y( rand() % SCREEN_H);
                }
            }
            if(HighScore.reset) // reset checks
            {
                Level_up.play(false);
                for(int w = 0; w < Game.enemies; w++)
                {
                    Cartrigdes[w].alive = true;
                    Cartrigdes[w].set_x( rand() % SCREEN_W - 32 );
                    Cartrigdes[w].set_y( rand() % SCREEN_H - 32 );
                }

                HighScore.reset_counter();
                HighScore.reset = false;
            }
            // collision, life, coordinates, and score sets and checks
            for(int b = 0; b < Game.enemies; b++)
            {
                if(Cartrigdes[b].alive)
                {
                    if(p_mouse_x != mouse_x || p_mouse_y != mouse_y)
                    {
                        Cartrigdes[b].AI_Avoid(40,mouse_x,mouse_y);
                    }
                    if(Collide.check(Cartrigdes[b].get_x(),Cartrigdes[b].get_y(), 32, 32,
                    mouse_x, mouse_y, 32, 32))
                    {
                        Grab.play(false);
                        HighScore.Set_Score();
                        Cartrigdes[b].alive = false;
                    }
                }
                if(Cartrigdes[b].alive)
                {
                    if( Cartrigdes[b].get_x() + 32 > SCREEN_W)
                    {
                        Cartrigdes[b].set_x(SCREEN_W - 32);
                    }
                    if( Cartrigdes[b].get_x() < 0)
                    {
                        Cartrigdes[b].set_x(0);
                    }
                    if( Cartrigdes[b].get_y() + 32 > SCREEN_H)
                    {
                        Cartrigdes[b].set_y(SCREEN_H - 32);
                    }
                    if( Cartrigdes[b].get_y() < 0)
                    {
                        Cartrigdes[b].set_y(0);
                    }
                }
            }

            elasped_time--;
            clear_keybuf();
        }
        acquire_screen();
        //draw functions go here...
        blit(background_game.picture,buffer,0,0,0,0,SCREEN_W,SCREEN_H);


        for(int a = 0; a < Game.enemies; a++)
        {
            if(Cartrigdes[a].alive)
            {
                Cartrigdes[a].Draw(buffer);
            }
        }
        textprintf(buffer, font, 0,0, makecol(255,255,255),"Rank: ");

        if(HighScore.score < 250)
        {
            textprintf(buffer, font,40,0,makecol(rand() % 255,rand() % 255, rand() % 255),
            "F.N.G.");
        }

        if(HighScore.score < 500 && HighScore.score >=250)
        {
            textprintf(buffer, font,40,0,makecol(rand() % 255,rand() % 255,rand() % 255),
            "Quick Draw!");
        }
        if(HighScore.score < 1000 && HighScore.score >=500)
        {
            textprintf(buffer, font,40,0,makecol(rand() % 255,rand() % 255,rand() % 255),
            "Hand Faction!");
        }

        if(HighScore.score < 1500 && HighScore.score >=1000)
        {
            textprintf(buffer, font,40,0,makecol(rand() % 255,rand() % 255,rand() % 255),
            "Cartridge thief!");
        }

        if(HighScore.score < 2000 && HighScore.score >=1500)
        {
            textprintf(buffer, font,40,0,makecol(rand() % 255,rand() % 255,rand() % 255),
            "Wii master!");
        }

        if(HighScore.score >= 2000)
        {
            textprintf(buffer, font,40,0,makecol(rand() % 255,rand() % 255,rand() % 255),
            "%i Kill Streak! Memory address standing by on your mark!",HighScore.score / 25);
        }
        textprintf(buffer, font, 500,0, makecol(255,255,255),"Score: %i", HighScore.score);

        Mouse.draw_trans_pic(buffer,mouse_x,mouse_y);

        blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
        release_screen();

        p_mouse_x = mouse_x;
        p_mouse_y = mouse_y;

        release_screen();
    }

    destroy_bitmap(buffer);
    return 0;
}
END_OF_MAIN();
